package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组服务
 */

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    //新增检查组,同时需要让检查组关联到检查项
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //操作t_checkgroup表
        checkGroupDao.add(checkGroup);
        //设置检查组和检查项的多对多的关联关系t_checkgroup_chueckitem表
        Integer checkGroupId = checkGroup.getId();
        //System.out.println(checkGroupId);
        this.setCheckGroupAndCheckItem(checkGroupId,checkitemIds);
    }

    //检查组分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id查询检查组
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    //根据检查组id查询检查组包含的多个检查项id
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    //编辑检查组同时关联检查项
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //修改基本信息
        checkGroupDao.edit(checkGroup);
        //,然后先把原来的关联关系清除
        checkGroupDao.deleteAssocication(checkGroup.getId());
        //,然后再建立关联关系
        Integer checkGroupId = checkGroup.getId();
        //System.out.println(checkGroupId);
        this.setCheckGroupAndCheckItem(checkGroupId,checkitemIds);
    }

    //删除检查组
    @Override
    public void delete(Integer id) {
        //先把原来的关联关系清除
        checkGroupDao.deleteAssocication(id);
        //然后删除检查组
        checkGroupDao.delete(id);
    }

    //查询所有的检查组
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    //建立检查组和检查项多对多的关系
    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds){
        if (checkitemIds !=null && checkitemIds.length>0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
